//
//  UserViewController.m
//  cm
//
//  Created by suyibaishan on 2017/10/25.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "UserViewController.h"
#import "CustomView.h"
#import "AmountView.h"
#import "CustomBtn.h"
#import "NoticeViewController.h"
#import "UserInfoViewController.h"
#import "RecommendViewController.h"
#import "GroupViewController.h"

#define tabBarHeight  self.tabBarController.tabBar.frame.size.height

@interface UserViewController ()
{
    UIScrollView *_scrollView;
    NSDictionary *dataSource;
    UIView *headView;
    UILabel *totalPriceLab;
    UILabel *withdrawAmountLab;
    UIImageView *tmImgView;
    UIView *earningsView;
    UILabel *earningsLabel;
    UILabel *totalAmountLab;
    UIView *awardView;
    CustomView *frankAwardView;
    CustomView *stationAwardView;
    CustomView *trainAwardView;
    CustomView *formAwardView;
    CustomView *starAwardView;
    CustomView *fundAwardView;
    UIView *depositView;
    UIView *infoView;
    AmountView *alreadyView;
    AmountView *cannotView;
}
@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    [self createUserView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:MUserCenter completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            dataSource = responseJson[@"data"];
            totalPriceLab.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"accountAmt"] floatValue]];
            withdrawAmountLab.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"availableCash"] floatValue]];
            earningsLabel.text = [NSString stringWithFormat:@"%.2f",[dataSource[@"todayAmount"] floatValue]];
            totalAmountLab.text = [NSString stringWithFormat:@"累计总收益+%.2f元",[dataSource[@"allAmount"] floatValue]];
            
            frankAwardView.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"directReward"] floatValue]];
            stationAwardView.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"postReward"] floatValue]];
            trainAwardView.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"nodeReward"] floatValue]];
            formAwardView.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"declarationReward"] floatValue]];
            starAwardView.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"starReward"] floatValue]];
            fundAwardView.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[dataSource[@"carFund"] floatValue]];

            alreadyView.priceLabel.text = [NSString stringWithFormat:@"%.2f",[dataSource[@"cashInCash"] floatValue]];
            alreadyView.titleLabel.text = [NSString stringWithFormat:@"复投%@次",dataSource[@"count"]];
            cannotView.priceLabel.text = [NSString stringWithFormat:@"%.2f",[dataSource[@"canNotCash"] floatValue]];
            cannotView.titleLabel.text = [NSString stringWithFormat:@"剩余积分%@",dataSource[@"points"]];
        }
//        else {
//            [self showMessage:responseJson[@"msg"]];
//        }
    }];
}

-(void)createUserView
{
    self.titleLabel.text = [NSString stringWithFormat:@"%@[VIP]",[mUserDefaults objectForKey:@"name"]];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight - tabBarHeight)];
    _scrollView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    [self createHeadView];
    
    [self createEarningsView];
    
    [self createAwardView];
    
    [self createDepositView];
    
    [self createInfoView];
    
    [_scrollView setupAutoContentSizeWithBottomView:infoView bottomMargin:0];
}

-(void)createHeadView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, IS_IPHONE_8PLUS?135:105)];
    headView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:headView];
    
    UILabel *totalLabel = [[UILabel alloc] init];
    totalLabel.font = [UIFont systemFontOfSize:13];
    totalLabel.text = @"总资产（元）";
    totalLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
    [headView addSubview:totalLabel];
    totalLabel.sd_layout
    .topSpaceToView(headView, IS_IPHONE_8PLUS?25:10)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    totalPriceLab = [[UILabel alloc] init];
    totalPriceLab.font = [UIFont systemFontOfSize:17];
//    totalPriceLab.text = @"¥10,000.00";
    totalPriceLab.textColor = [UIColor colorWithRed:0.99 green:0.28 blue:0.1 alpha:1];
    [headView addSubview:totalPriceLab];
    totalPriceLab.sd_layout
    .topSpaceToView(totalLabel, 0)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    UILabel *withdrawLabel = [[UILabel alloc] init];
    withdrawLabel.font = [UIFont systemFontOfSize:13];
    withdrawLabel.text = @"可提现金额";
    withdrawLabel.textColor = [UIColor colorWithRed:0.38 green:0.38 blue:0.38 alpha:1];
    [headView addSubview:withdrawLabel];
    withdrawLabel.sd_layout
    .topSpaceToView(totalPriceLab, 5)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    withdrawAmountLab = [[UILabel alloc] init];
    withdrawAmountLab.font = [UIFont systemFontOfSize:13];
//    withdrawAmountLab.text = @"¥1,110.00";
    withdrawAmountLab.textColor = [UIColor colorWithRed:0.99 green:0.28 blue:0.1 alpha:1];
    [headView addSubview:withdrawAmountLab];
    withdrawAmountLab.sd_layout
    .topSpaceToView(withdrawLabel, 0)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    tmImgView = [[UIImageView alloc] init];
//    tmImgView.userInteractionEnabled = YES;
//    tmImgView.layer.borderWidth = 1;
//    tmImgView.layer.borderColor = [UIColor whiteColor].CGColor;
//    tmImgView.clipsToBounds = YES;
    tmImgView.image = [UIImage imageNamed:@"cm_tm"];
    [headView addSubview:tmImgView];
    tmImgView.sd_layout
    .topSpaceToView(headView, IS_IPHONE_8PLUS?38:23)
    .rightSpaceToView(headView, 5)
    .widthIs(200)
    .heightIs(59);
}

-(void)createEarningsView
{
    earningsView = [[UIView alloc] init];
    earningsView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:earningsView];
    earningsView.sd_layout
    .topSpaceToView(headView, 10)
    .leftSpaceToView(_scrollView, 0)
    .widthIs(kScreenWidth)
    .heightIs(IS_IPHONE_8PLUS?125:95);
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:13];
    titleLabel.text = @"今日收益";
    titleLabel.textColor = [UIColor colorWithRed:0.99 green:0.47 blue:0.41 alpha:1];
    [earningsView addSubview:titleLabel];
    titleLabel.sd_layout
    .topSpaceToView(earningsView, IS_IPHONE_8PLUS?25:10)
    .leftSpaceToView(earningsView, (kScreenWidth-100)/2)
    .widthIs(100)
    .heightIs(20);
    
    earningsLabel = [[UILabel alloc] init];
    earningsLabel.textAlignment = NSTextAlignmentCenter;
    earningsLabel.font = [UIFont boldSystemFontOfSize:32];
//    earningsLabel.text = @"100.00";
    earningsLabel.textColor = [UIColor colorWithRed:0.99 green:0.28 blue:0.1 alpha:1];
    [earningsView addSubview:earningsLabel];
    earningsLabel.sd_layout
    .topSpaceToView(titleLabel, 0)
    .leftSpaceToView(earningsView, 50)
    .widthIs(kScreenWidth - 100)
    .heightIs(35);
    
    totalAmountLab = [[UILabel alloc] init];
    totalAmountLab.textAlignment = NSTextAlignmentCenter;
    totalAmountLab.font = [UIFont systemFontOfSize:13];
//    totalAmountLab.text = @"累计总收益+1,110.00元";
    totalAmountLab.textColor = [UIColor colorWithRed:0.99 green:0.47 blue:0.41 alpha:1];
    [earningsView addSubview:totalAmountLab];
    totalAmountLab.sd_layout
    .topSpaceToView(earningsLabel, 0)
    .leftSpaceToView(earningsView, 50)
    .widthIs(kScreenWidth - 100)
    .heightIs(20);
}

-(void)createAwardView
{
    awardView = [[UIView alloc] init];
    awardView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:awardView];
    awardView.sd_layout
    .topSpaceToView(earningsView, 10)
    .leftSpaceToView(_scrollView, 0)
    .widthIs(kScreenWidth)
    .heightIs(120);
    
    frankAwardView = [[CustomView alloc] init];
    frankAwardView.titleLabel.text = @"直推奖励";
    [awardView addSubview:frankAwardView];
    frankAwardView.sd_layout
    .topSpaceToView(awardView, 0)
    .leftSpaceToView(awardView, 0)
    .widthIs(kScreenWidth/3)
    .heightIs(60);
    
    stationAwardView = [[CustomView alloc] init];
    stationAwardView.titleLabel.text = @"岗位奖励";
    [awardView addSubview:stationAwardView];
    stationAwardView.sd_layout
    .topSpaceToView(awardView, 0)
    .leftSpaceToView(frankAwardView, 0)
    .widthIs(kScreenWidth/3)
    .heightIs(60);
    
    trainAwardView = [[CustomView alloc] init];
    trainAwardView.titleLabel.text = @"培养奖励";
    [awardView addSubview:trainAwardView];
    trainAwardView.sd_layout
    .topSpaceToView(awardView, 0)
    .leftSpaceToView(stationAwardView, 0)
    .widthIs(kScreenWidth/3)
    .heightIs(60);
    
    formAwardView = [[CustomView alloc] init];
    formAwardView.titleLabel.text = @"报单奖励";
    [awardView addSubview:formAwardView];
    formAwardView.sd_layout
    .topSpaceToView(frankAwardView, 0)
    .leftSpaceToView(awardView, 0)
    .widthIs(kScreenWidth/3)
    .heightIs(60);
    
    starAwardView = [[CustomView alloc] init];
    starAwardView.titleLabel.text = @"星级奖励";
    [awardView addSubview:starAwardView];
    starAwardView.sd_layout
    .topSpaceToView(frankAwardView, 0)
    .leftSpaceToView(formAwardView, 0)
    .widthIs(kScreenWidth/3)
    .heightIs(60);
    
    fundAwardView = [[CustomView alloc] init];
    fundAwardView.titleLabel.text = @"购车基金";
    [awardView addSubview:fundAwardView];
    fundAwardView.sd_layout
    .topSpaceToView(frankAwardView, 0)
    .leftSpaceToView(starAwardView, 0)
    .widthIs(kScreenWidth/3)
    .heightIs(60);
    
    UIView *transverseline = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth - 30, 0.5)];
    transverseline.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [awardView addSubview:transverseline];
    transverseline.sd_layout
    .leftSpaceToView(awardView, 0)
    .topSpaceToView(awardView, 60)
    .widthIs(kScreenWidth)
    .heightIs(1);
    
    for (NSInteger i = 0; i < 2; i++) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        [awardView addSubview:line];
        line.sd_layout
        .leftSpaceToView(awardView, kScreenWidth/3+i*kScreenWidth/3)
        .topSpaceToView(awardView, 0)
        .widthIs(1)
        .heightIs(120);
    }
}

-(void)createDepositView
{
    depositView = [[UIView alloc] init];
    depositView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:depositView];
    depositView.sd_layout
    .topSpaceToView(awardView, 10)
    .leftSpaceToView(_scrollView, 0)
    .widthIs(kScreenWidth)
    .heightIs(80);
    
    alreadyView = [[AmountView alloc] init];
    alreadyView.nameLabel.text = @"已提现金额（元）";
    [depositView addSubview:alreadyView];
    alreadyView.sd_layout
    .topSpaceToView(depositView, 0)
    .leftSpaceToView(depositView, 0)
    .widthIs(kScreenWidth/2)
    .heightIs(80);
    
    cannotView = [[AmountView alloc] init];
    cannotView.nameLabel.text = @"不可提金额（元）";
    [depositView addSubview:cannotView];
    cannotView.sd_layout
    .topSpaceToView(depositView, 0)
    .leftSpaceToView(alreadyView, 0)
    .widthIs(kScreenWidth/2)
    .heightIs(80);
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth - 30, 0.5)];
    line.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [depositView addSubview:line];
    line.sd_layout
    .leftSpaceToView(depositView, kScreenWidth/2)
    .topSpaceToView(depositView, 10)
    .widthIs(1)
    .heightIs(60);
}

-(void)createInfoView
{
    infoView = [[UIView alloc] init];
    infoView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:infoView];
    infoView.sd_layout
    .topSpaceToView(depositView, 10)
    .leftSpaceToView(_scrollView, 0)
    .widthIs(kScreenWidth)
    .heightIs(90);
    
    NSArray *titleArr = @[@"车盟公告",@"个人信息",@"直荐非直荐",@"我的团队"];
    NSArray *normalImages = @[@"cm_zu",@"cm_person",@"person_two",@"person_three"];
    for (NSInteger i = 0; i < titleArr.count; i++) {
        CustomBtn *customBtn = [CustomBtn buttonWithType:UIButtonTypeCustom];
        [customBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [customBtn setImage:[UIImage imageNamed:normalImages[i]] forState:UIControlStateNormal];
        customBtn.tag = 1000 + i;
        [customBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [infoView addSubview:customBtn];
        customBtn.sd_layout
        .topSpaceToView(infoView, 5)
        .leftSpaceToView(infoView, 10+i*(kScreenWidth-20)/titleArr.count)
        .widthIs((kScreenWidth-20)/titleArr.count)
        .heightIs(70);
    }
}

-(void)clickButton:(UIButton *)button
{
    if (button.tag == 1000) {
        NoticeViewController *noticeVC = [[NoticeViewController alloc] init];
        [self.navigationController pushViewController:noticeVC animated:YES];
    }
    else if (button.tag == 1001) {
        UserInfoViewController *userInfoVC = [[UserInfoViewController alloc] init];
        [self.navigationController pushViewController:userInfoVC animated:YES];
    }
    else if (button.tag == 1002) {
        RecommendViewController *recommendVC = [[RecommendViewController alloc] init];
        [self.navigationController pushViewController:recommendVC animated:YES];
    }
    else {
        GroupViewController *groupVC = [[GroupViewController alloc] init];
        [self.navigationController pushViewController:groupVC animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reloadData];
    self.titleLabel.text = [NSString stringWithFormat:@"%@[VIP]",[mUserDefaults objectForKey:@"name"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
